<template>
  <el-row>
    <el-col id="histogram" style="width:500px;height:380px;" class="pie"></el-col>
  </el-row>
</template>
<script lang="ts">
import { defineComponent, onMounted, getCurrentInstance } from 'vue'

export default defineComponent({
  name: 'histogram',
  setup(props, { emit }) {
    onMounted(() => {
      const { proxy }: any = getCurrentInstance()
      // 基于准备好的dom，初始化echarts实例
      let myChart = proxy.$echarts.init(document.getElementById('histogram'))
      let dataAxis = [
        '点',
        '击',
        '柱',
        '子',
        '或',
      ]
      // prettier-ignore
      let data = [220, 182, 191, 234, 290, 330];
      let yMax = 500
      let dataShadow = []
      for (let i = 0; i < data.length; i++) {
        dataShadow.push(yMax)
      }
      let option = {
        // title: {
        //   text: '特性示例：渐变色 阴影 点击缩放',
        //   subtext: 'Feature Sample: Gradient Color, Shadow, Click Zoom',
        // },
        xAxis: {
          data: dataAxis,
          axisLabel: {
            inside: true,
            color: '#fff',
          },
          axisTick: {
            show: false,
          },
          axisLine: {
            show: false,
          },
          z: 10,
        },
        yAxis: {
          axisLine: {
            show: false,
          },
          axisTick: {
            show: false,
          },
          axisLabel: {
            color: '#999',
          },
        },
        dataZoom: [
          {
            type: 'inside',
          },
        ],
        series: [
          {
            type: 'bar',
            showBackground: true,
            itemStyle: {
              color: new proxy.$echarts.graphic.LinearGradient(0, 0, 0, 1, [
                { offset: 0, color: '#83bff6' },
                { offset: 0.5, color: '#188df0' },
                { offset: 1, color: '#188df0' },
              ]),
            },
            emphasis: {
              itemStyle: {
                color: new proxy.$echarts.graphic.LinearGradient(0, 0, 0, 1, [
                  { offset: 0, color: '#2378f7' },
                  { offset: 0.7, color: '#2378f7' },
                  { offset: 1, color: '#83bff6' },
                ]),
              },
            },
            data: data,
          },
        ],
      }
      // Enable data zoom when user click bar.
      const zoomSize = 6
      myChart.on('click', function (params:any) {
        console.log(dataAxis[Math.max(params.dataIndex - zoomSize / 2, 0)])
        myChart.dispatchAction({
          type: 'dataZoom',
          startValue: dataAxis[Math.max(params.dataIndex - zoomSize / 2, 0)],
          endValue:
            dataAxis[
              Math.min(params.dataIndex + zoomSize / 2, data.length - 1)
            ],
        })
      })
      // 使用刚指定的配置项和数据显示图表。
      myChart.setOption(option)
    })
  },
})
</script>

<style lang="scss" scoped>
.pie {
  border: 1px solid #fff;
  border-radius: 5px;
}
</style>